package com.lsh.oms.core.service.order.frozen;

import org.springframework.stereotype.Service;

/**
 * 订单发货service
 *
 * @author peter
 */
@Service
@Deprecated
public class OrderDeliveryFrozenService {

//    private static Logger logger = LoggerFactory.getLogger(OrderDeliveryFrozenService.class);
//
//    @Autowired
//    private ShippingOrderService shippingService;

//    @Autowired
//    private GoodsServiceProxy goodsServiceProxy;

//    @Autowired
//    private EsProxy esProxy;


//    /**
//     * 发货单
//     *
//     * @param orderShippingHeadDto 请求信息
//     * @param orderHead            订单信息
//     * @param logSign              唯一标识
//     * @return 返回值
//     */
//    @Transactional(rollbackFor = RuntimeException.class)
//    public CommonResult<Object> createShippingOrder(OrderShippingHeadDto orderShippingHeadDto, OrderHead orderHead, String logSign) {
//        logger.info(logSign + "发货单请求发货参数" + JsonUtils.obj2Json(orderShippingHeadDto));
//        //TODO:发货单标记
//
//        if (orderShippingHeadDto.getShippingOrderId() == null || "".equals(orderShippingHeadDto.getShippingOrderId())) {
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "请输入发货单号.请查询");
//        }
//
//        for (OrderShippingDetailDto detail : orderShippingHeadDto.getDetails()) {
//            if (detail == null || StringUtils.isBlank(detail.getCode()) || detail.getRealQty() == null || detail.getRealQty().compareTo(BigDecimal.ZERO) <= 0) {
//                throw new BusinessException(ReturnCode.REQUEST_FAIL, "明细中有没有物美码或者没有数量，请核实");
//            }
//        }
//
//        Long orderCode = orderHead.getOrderCode();
//        orderShippingHeadDto.setOrderId(orderCode);
//
//        JSONObject extjson = JSON.parseObject(orderHead.getExt());
//
//        String providerId = extjson.getString("providerId");
//        if (StringUtils.isBlank(providerId)) {
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "订单信息中没有供商信息，请联系相关人员处理");
//        }
//
//        //请求商品服务
//        Map<String, Good> code2GoodsMaps = this.goodsServiceProxy.requestCode2Goods(orderCode, orderHead.getParentOrderCode(), providerId, orderHead.getVenderId(), logSign);
//        //请求计算服务
//        CalResultVo calVo = this.requestCount(code2GoodsMaps, orderShippingHeadDto, logSign,orderHead.getVenderId());
//        OrderShippingHeadWithBLOBs osh = OrderFactory.createShippingHead(orderShippingHeadDto, orderHead, calVo);
//        List<OrderShippingDetail> details = this.getOrderShippingDetails(osh, calVo, code2GoodsMaps);
//
//        boolean successFlag;
//        try {
//            successFlag = false;
////                    shippingService.dealShipping(osh, details, logSign);
//        } catch (Throwable e) {
//            logger.info(logSign + "发货单保存数据库失败，订单号:" + orderCode);
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "发货单保存数据库失败");
//        }
//        //通知es
//        this.notifyEs4frozen(orderCode, OrderFactory.getMarketName(orderHead), OrderFactory.getProviderId(orderHead), logSign);
//
//        return this.initCommonResult(successFlag, orderHead, calVo);
//    }


//    /**
//     * 发货单
//     *
//     * @param orderShippingHeadDto 请求信息
//     * @param orderHead            订单信息
//     * @param logSign              唯一标识
//     * @return 返回值
//     */
//    @Transactional(rollbackFor = RuntimeException.class)
//    public CommonResult<Object> createShippingOrder4Frogen(OrderShippingHeadDto orderShippingHeadDto, OrderHead orderHead, String logSign) {
//        logger.info(logSign + "发货单请求发货参数" + JsonUtils.obj2Json(orderShippingHeadDto));
//        //TODO:发货单标记
//        if (orderShippingHeadDto.getShippingOrderId() == null || "".equals(orderShippingHeadDto.getShippingOrderId())) {
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "请输入发货单号.请查询");
//        }
//        for (OrderShippingDetailDto detail : orderShippingHeadDto.getDetails()) {
//            if (detail == null || detail.getRealQty() == null || detail.getRealQty().compareTo(BigDecimal.ZERO) <= 0) {
//                throw new BusinessException(ReturnCode.REQUEST_FAIL, "明细中有没有物美码或者没有数量，请核实");
//            }
//        }
//        Long orderCode = orderHead.getOrderCode();
//        orderShippingHeadDto.setOrderId(orderCode);
//        orderShippingHeadDto.setZoneId(orderHead.getRegionCode());
//
//        JSONObject extjson = JSON.parseObject(orderHead.getExt());
//        String providerId = extjson.getString("providerId");
//        if (StringUtils.isBlank(providerId)) {
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "订单信息中没有供商信息，请联系相关人员处理");
//        }
//        //请求商品服务
//        Map<Long, Good> item2GoodsMap = this.goodsServiceProxy.requestItem2Goods(orderCode, orderHead.getParentOrderCode(), providerId, orderHead.getVenderId(), logSign);
//        //请求计算服务
//        CalResultVo calVo = this.requestCount4Frogen(item2GoodsMap, orderShippingHeadDto, logSign,orderHead.getVenderId());
//        OrderShippingHeadWithBLOBs osh = OrderFactory.createShippingHead(orderShippingHeadDto, orderHead, calVo);
//        List<OrderShippingDetail> details = this.getOrderShippingDetails4Frogen(osh, calVo, item2GoodsMap);
//        boolean successFlag;
//        try {
//            successFlag = false;
////                    this.shippingService.dealShipping(osh, details, logSign);
//        } catch (Exception e) {
//            logger.error("发货单保存数据库失败,异常信息: " + e.getMessage(), e);
//            logger.info(logSign + "发货单保存数据库失败，订单号:" + orderCode);
//            e.printStackTrace();
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "发货单保存数据库失败");
//        }
//        //通知es
//        this.notifyEs4frozen(orderCode, OrderFactory.getMarketName(orderHead), OrderFactory.getProviderId(orderHead), logSign);
//
//        return this.initCommonResult(successFlag, orderHead, calVo);
//    }

//    /**
//     * @param successFlag
//     * @param orderHead
//     * @param calVo
//     * @return
//     */
//    private CommonResult<Object> initCommonResult(boolean successFlag, OrderHead orderHead, CalResultVo calVo) {
//
//        Map<String, Object> data = new HashMap<>(10);
//        data.put("flag", successFlag);
//        BigDecimal sourceMoney = new BigDecimal(calVo.getHead().getMoney());
//
//        BigDecimal realPayMoney = sourceMoney;
//        if (OrderFactory.getUserType(orderHead) == UserType.KA.getIndex()) {
//            data.put("real_pay_money", BigDecimal.ZERO);
//            data.put("money", sourceMoney);
//        } else {
//            data.put("real_pay_money", realPayMoney);
//            data.put("money", realPayMoney);
//        }
//
//        BigDecimal saledMoney = new BigDecimal(calVo.getHead().getDiscounted_money());
//        BigDecimal couponMoney = saledMoney.subtract(realPayMoney);
//        data.put("total_coupon_money", couponMoney);
//
//        CommonResult<Object> commonResult = new CommonResult<>();
//        commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
//        commonResult.setData(data);
//
//        return commonResult;
//    }

//    /**
//     * 请求计算服务
//     *
//     * @param code2GoodsMaps       商品信息
//     * @param orderShippingHeadDto 发货信息
//     * @param logSign              唯一标识
//     * @return 计算结果
//     */
//    private CalResultVo requestCount(Map<String, Good> code2GoodsMaps, OrderShippingHeadDto orderShippingHeadDto, String logSign, Long venderId) {
//        //请求计算服务
//        for (OrderShippingDetailDto shippingDetailDto : orderShippingHeadDto.getDetails()) {
//            Good good = code2GoodsMaps.get(shippingDetailDto.getCode());
//            if (good == null) {
//                logger.info(logSign + "商品不在此订单中，处理失败，商品码:" + shippingDetailDto.getCode());
//                throw new BusinessException(ReturnCode.REQUEST_FAIL, "商品不在此订单中，处理失败，商品码:" + shippingDetailDto.getCode());
//            }
//            logger.info(logSign + JsonUtils.obj2Json(good));
//            shippingDetailDto.setItemId(good.getSkuCode());
//            shippingDetailDto.setSkuId(good.getGoodsCode());
//        }
//
//        return this.request(orderShippingHeadDto, logSign, venderId);
//    }


//    /**
//     * 请求计算服务
//     *
//     * @param codeMaps             商品信息
//     * @param orderShippingHeadDto 发货信息
//     * @param logSign              唯一标识
//     * @return 计算结果
//     */
//    private CalResultVo requestCount4Frogen(Map<Long, Good> codeMaps, OrderShippingHeadDto orderShippingHeadDto, String logSign, Long venderId) {
//        //请求计算服务
//        for (OrderShippingDetailDto shippingDetailDto : orderShippingHeadDto.getDetails()) {
//            logger.info("codeMaps is {}", JSON.toJSONString(codeMaps));
//
//            Good good = codeMaps.get(shippingDetailDto.getSkuId());
//            if (good == null) {
//                logger.info(logSign + "商品不在此订单中，处理失败，链商商品码:" + shippingDetailDto.getSkuId());
//                throw new BusinessException(ReturnCode.REQUEST_FAIL, "商品不在此订单中，处理失败，商品码:" + shippingDetailDto.getSkuId());
//            }
//            logger.info(logSign + JsonUtils.obj2Json(good));
//            shippingDetailDto.setCode(good.getSupplySkuCode());
//        }
//
//        return this.request(orderShippingHeadDto, logSign, venderId);
//    }

//    /**
//     * @param orderShippingHeadDto
//     * @param logSign
//     * @return
//     */
//    private CalResultVo request(OrderShippingHeadDto orderShippingHeadDto, String logSign, Long venderId) {
//
//        CalResultVo calVo = ComputeServiceUtils.getCalService4shipping(orderShippingHeadDto, logSign, calUrl, venderId);
//        logger.info(logSign + "请求计算服务返回信息" + JsonUtils.obj2Json(calVo));
//        if (calVo == null || calVo.getHead() == null || calVo.getDetail() == null || calVo.getDetail().size() == 0) {
//            logger.info(logSign + ",计算服务获取信息为空,订单号:" + orderShippingHeadDto.getOrderId());
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "计算服务获取信息为空");
//        }
//
//        return calVo;
//    }


//    /**
//     * 组织签收单详情列表
//     *
//     * @param osh            签收单详情
//     * @param calVo          计算服务数据
//     * @param code2GoodsMaps 商品服务数据
//     * @return 返回数据
//     */
//    private List<OrderShippingDetail> getOrderShippingDetails(OrderShippingHeadWithBLOBs osh, CalResultVo calVo, Map<String, Good> code2GoodsMaps) {
//        List<CalShippingDetailVo> detailVoList = calVo.getDetail();
//        List<OrderShippingDetail> details = new ArrayList<>(detailVoList.size());
//        String wmCode;
//        int sysTime = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
//        for (CalShippingDetailVo detailVo : detailVoList) {
//            OrderShippingDetail detail = new OrderShippingDetail(detailVo);
//            detail.setShippingOrderId(osh.getShippingOrderId());
//            String ext = detailVo.getExt();
//            if (StringUtils.isBlank(ext)) {
//                logger.info("计算服务返回");
//                return Collections.EMPTY_LIST;
//            }
//
//            com.alibaba.fastjson.JSONObject detailExt = com.alibaba.fastjson.JSONObject.parseObject(ext);
//
//            String isWeighingGoods = detailExt.getString("is_weighing_goods");
//            if (StringUtils.isBlank(isWeighingGoods)) {
//                isWeighingGoods = "0";
//            }
//
//            detailExt.put("is_weighing_goods", isWeighingGoods);
//
//            detail.setExt(detailExt.toJSONString());
//            wmCode = detailExt.getString("wm_code");
//
//            detail.setQty(new BigDecimal(detailVo.getQty()));
//            Good good = code2GoodsMaps.get(wmCode);
//            detail.setItemId(good.getSkuCode());
//            detail.setCode(wmCode);
//            detail.setCreatedAt(sysTime);
//            detail.setUpdatedAt(sysTime);
//            detail.setZoneId(osh.getZoneId());
//            if (osh.getZoneId() == 1000) {
//                detail.setSupplierOrg(2);
//            } else {
//                detail.setSupplierOrg(3);
//            }
//
//            Integer itemCode = detailVo.getItemCode();
//            if (null == itemCode) {
//                itemCode = 0;
//            }
//            detail.setItemCode(itemCode);
//
//            details.add(detail);
//        }
//
//        return details;
//    }
//
//    /**
//     * 组织签收单详情列表
//     *
//     * @param osh           签收单详情
//     * @param calVo         计算服务数据
//     * @param item2GoodsMap 商品服务数据
//     * @return 返回数据
//     */
//    private List<OrderShippingDetail> getOrderShippingDetails4Frogen(OrderShippingHeadWithBLOBs osh, CalResultVo calVo, Map<Long, Good> item2GoodsMap) {
//        List<CalShippingDetailVo> detailVoList = calVo.getDetail();
//        List<OrderShippingDetail> details = new ArrayList<>(detailVoList.size());
//        int sysTime = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
//        for (CalShippingDetailVo detailVo : detailVoList) {
//            OrderShippingDetail detail = new OrderShippingDetail(detailVo);
//            detail.setShippingOrderId(osh.getShippingOrderId());
//            try {
//                JSONObject jsonObject = JSONObject.parseObject(detailVo.getExt());
//                detail.setExt(jsonObject.toJSONString());
//            } catch (Exception e) {
//                logger.error("detailVo.getExt() is null", e);
//            }
//
//            detail.setQty(new BigDecimal(detailVo.getQty()));
//            Good good = item2GoodsMap.get(Long.parseLong(detailVo.getSkuId()));
//            detail.setItemId(good.getSkuCode());
//            detail.setCode(good.getSupplySkuCode());
//            detail.setCreatedAt(sysTime);
//            detail.setUpdatedAt(sysTime);
//            detail.setZoneId(osh.getZoneId());
//            detail.setSupplierOrg(good.getSupplierOrg());
//            Integer itemCode = detailVo.getItemCode();
//            if (null == itemCode) {
//                itemCode = 0;
//            }
//            detail.setItemCode(itemCode);
//
//            details.add(detail);
//        }
//
//        return details;
//    }
//
//    /**
//     * 通知ES
//     *
//     * @param orderCode 订单号
//     * @param logSign   唯一标识
//     */
//    private void notifyEs4frozen(Long orderCode, String marketName, String providerId, String logSign) {
//        try {
//            OrderShippingHeadWithBLOBs shippingEs = shippingService.getBolbs(orderCode);
//
//            logger.info(logSign + "ES入参新建发货索引参数是:" + JsonUtils.obj2Json(shippingEs));
//            shippingEs.setMarketName(marketName);
//            shippingEs.setProviderId(providerId);
//            String addressInfo = shippingEs.getAddressInfo();
//
//            if (StringUtils.isNotBlank(addressInfo)) {
//                JSONObject address = JSON.parseObject(addressInfo);
//                shippingEs.setAddressTelephone(address.getString("contact_phone"));
//            }
//
//            logger.info(logSign + "ES入参新建发货索引参数all是:" + JsonUtils.obj2Json(shippingEs));
//            esProxy.createIndex(shippingEs, logSign);
//            OrderHeadForEs ohEs = new OrderHeadForEs();
//            ohEs.setOrderCode(orderCode);
//            ohEs.setOrderStatus(OrderStatus.ORDER_DELIVERY.getIndex());
//            logger.info(logSign + "ES入参发货单是:" + JsonUtils.obj2Json(ohEs));
//            esProxy.editIndex(ohEs, logSign);
//        } catch (Exception e) {
//            logger.info(logSign + "订单号" + orderCode + ",生成发货单同步es失败。");
//            logger.info(e.getMessage(), e);
//        }
//    }

}
